#!/bin/sh

die() {
    echo "$*" >&2
    exit 1
}

#
# Remove the artifacts directory.
#
rm -rf xcode-artifacts

#
# Remove the artifacts directory.
#
rm -rf xcode-build-auto

appname='Voxper'
config='Release'
sdk='iphoneos4.3'

echo using configuration $config

echo updating version number
agvtool bump -all
fullversion="$(agvtool mvers -terse1)($(agvtool vers -terse))"
echo building version $fullversion

xcodebuild -alltargets \
			-project Voxper.xcodeproj \
			SYMROOT=`pwd`/xcode-build-auto \
			-configuration "$config" \
			-sdk $sdk build || die "build failed"

#
# If the build succeeded, copy the build artifacts over.
#

mkdir xcode-artifacts;
cd xcode-artifacts;
mkdir appstore;
mkdir adhoc;
cd ..;
# copy the .dSYM folder
cd xcode-build-auto/"$config"-iphoneos;
cp -Rp JMobi.app.dSYM ../../xcode-artifacts;

echo making ipa...
# packaging

#zip -r $ipaname Payload

xcrun -sdk $sdk PackageApplication -v "JMobi.app" -o `pwd`/JMobi-Adhoc.ipa --sign "iPhone Developer: Syed Naqvi (2532DX94TF)" --embed "../../Provisioning/Adhoc.mobileprovision"
xcrun -sdk $sdk PackageApplication -v "JMobi.app" -o `pwd`/JMobi.ipa --sign "iPhone Developer: Syed Naqvi (2532DX94TF)" --embed "../../Provisioning/AppStore.mobileprovision"

# copy the current artifacts
cp JMobi-Adhoc.ipa ../../xcode-artifacts/adhoc;
cp JMobi.ipa ../../xcode-artifacts/appstore;
cd ../..;

cp Provisioning/Adhoc.mobileprovision xcode-artifacts/adhoc;
cp Provisioning/AppStore.mobileprovision xcode-artifacts/appstore;

echo finished making IPA
exit -1